<template>
<div id="choose-user">
    <header id="header">
        <div class="head-img" @click="handleClose">
            <img 
                :class="{'no-gray': $root.userSelf.isOnline}"
                :src="$root.userSelf.headImg"
                alt=""
            >
        </div>
        <h2 class="header-title">好友列表</h2>
    </header>
    <ul class="chat-item">
        <li 
            class="list"
            v-for="item in friend"
            :key="item.id"
            @click="handleChooseUserToChat(item)"
        >
            <img :class="{'no-gray': item.isOnline}" :src="item.headImg" alt="">
            <h3 class="username">{{item.username}}</h3>
            <i :class="[~unreceivedNameList.indexOf(item.username)? 'red-dot' : '']"></i>
        </li>
    </ul>
</div>
</template>

<script>
export default {
    name: "chooseUser",
    props: ["friends", "unreceivedNameList"],
    data() {
        return {
            charUser: null,
            flags: true
        }
    },
    mounted() {
        console.log(this.$root.userSelf.username)
    },
    methods: {
        // 选择用户聊天
        handleChooseUserToChat(user) {
            localStorage.setItem("currentUser", JSON.stringify(user))
            this.chatUser = user
            // this.isChat = true
            this.$emit("tochat", {flag: true, chatUser: user})
            let len = 1
            this.unreceivedNameList.forEach(item => {
                if(item === user.username) {
                    len++
                }
            })
            this.unreceivedNameList.sort()
            this.unreceivedNameList.splice(this.unreceivedNameList.indexOf(user.username), len)
        },

        handleClose() {
            this.socket.emit("disconnect")
            this.$emit("close", false)
        }
    },
    computed: {
        friend() {
            return this.friends.filter(item => this.$root.userSelf.username !== item.username)
        }
    },
}
</script>

<style scoped>
#choose-user {
    width: 100vw;
    height: 100vh;
}
#header {
    position: relative;
    width: 100vw;
    height: 50px;
    line-height: 50px;
    text-align: center;
    background-color: #bfffda;
}
#header .head-img {
    position: absolute;
    top: 0;
    left: 0;
    width: 50px;
    height: 50px;
    padding-top: 2px;
    padding-left: 5px;
    box-sizing: border-box;
    cursor: pointer;
}
#header .head-img img,
.chat-item .list img {
    width: 45px;
    height: 45px;
    border-radius: 50%;
}

.chat-item .list {
    position: relative;
    display: flex;
    flex-direction: row;
    align-items: center;
    height: 60px;
    line-height: 60px;
    padding: 0 5px;
    border-bottom: 1px solid #ccc;
    box-sizing: border-box;
    cursor: pointer;
}
.chat-item .list img {
    filter: grayscale(1);
}
.chat-item .list .no-gray {
    filter: grayscale(0);
}
.chat-item .list .red-dot::before {
    position: absolute;
    display: block;
    bottom: 10px;
    left: 50px;
    width: 8px;
    height: 8px;
    background-color: red;
    border-radius: 50%;
    filter: grayscale(0) !important;
    content: "";
}
.chat-item .list>.username{
    overflow: hidden;
    margin-left: 15px;
    color: #323232;
    white-space: nowrap;
    text-overflow: ellipsis;
}
</style>